package com.xiaomuedu.controller;

import java.util.List;

import com.jfinal.aop.Before;
import com.jfinal.aop.Clear;
import com.jfinal.core.Controller;
import com.jfinal.plugin.ehcache.CacheInterceptor;
import com.jfinal.plugin.ehcache.CacheKit;
import com.jfinal.plugin.ehcache.CacheName;
import com.jfinal.plugin.ehcache.IDataLoader;
import com.xiaomuedu.common.model.User;
/**
 * 用户CURD
 * @author 小木学堂
 *
 */
@Clear
public class UserController extends Controller {
	/**
	 * 直接访问user地址进入list.jsp
	 */
/*	@Before(CacheInterceptor.class)
	@CacheName("xiaomuedu")*/
	public void index(){
		/*List<User> users=CacheKit.get("xiaomuedu", "users");
		if(users==null){
			users=User.dao.find("select * from user");
			CacheKit.put("xiaomuedu", "users", users);
		}*/
		List<User> users=CacheKit.get("xiaomuedu", "users", new IDataLoader() {
			@Override
			public Object load() {
				return User.dao.find("select * from user");
			}
		});
		setAttr("users", users);
		System.out.println("得到数据"+users.size()+"个");
		render("list.jsp");
	}
	/**
	 * 访问user/form 地址进入form.jsp
	 */
	public void form(){
		Integer id=getParaToInt(0);
		if(id!=null&&id>0){
			setAttr("user", User.dao.findById(id));
		}
		render("form.jsp");
	}
	/**
	 * 数据提交
	 */
	public void submit(){
		User user=getModel(User.class,"user");
		user.save();
		redirect("/user");
	}
	public void update(){
		User user=getModel(User.class,"user");
		user.update();
		redirect("/user");
	}
	public void search(){
		List<User> users=User.dao.find("select * from user where name like '%"+getPara("keywords")+"%' or remark like '%"+getPara("keywords")+"%'");
		setAttr("users", users);
		keepPara();
		System.out.println("得到数据"+users.size()+"个");
		render("list.jsp");
	}
	public void edit(){
		form();
	}
	public void del(){
		User.dao.deleteById(getPara(0));
		redirect("/user");
	}
	
	
}
